﻿(function () {

    appModule.controller('host.views.editions.index', [
        '$scope', '$uibModal', 'uiGridConstants', 'abp.services.app.edition','abp.services.app.test',
        function ($scope, $uibModal, uiGridConstants, editionService, testService) {
            var vm = this;
            vm.testComponentData = null;
            //数据测试
            testService.getTestsAsync().success(function (result) {
                console.log('result:', result);
                vm.testComponentData = result.items;
            });
            //单选变量
            vm.testRadio = "key2";
            //多选变量
            vm.testCheckBox = {
                "key1":true //设置默认值
            };
            //下拉框配置
            vm.testDropdownBox = null;
            vm.selectedItem = null;
            var urlTest = "/api/services/app/test/"; //获取数据的地址
            var testDataSource = new kendo.data.DataSource({ //实例化数据源
                type: 'aspnetmvc-ajax', //请求类型
                transport: {
                    read: {//读取模式
                        url: urlTest + "KendoGetTests", 
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                serverFiltering: true,
                schema: {
                    data: function (data) {
                        if (data.success == true && data.result !== null) {
                            vm.testDropdownBox = data.result.data;
                            if (vm.testDropdownBox.length == 0) {
                                vm.selectedItem = null;
                            }
                            return vm.testDropdownBox;
                        }
                        return {};
                    },
                },
                filter: {
                    field: "",
                    operator: "",
                    value: {},
                    logic: "",
                    filters: [

                    ]
                }
            });
            vm.testTypesOptions = {
                dataSource: testDataSource,//数据源
                optionLabel: "选择类型",
                dataTextField: "value",//显示字段
                dataValueField: "key",//值
                //index: 0,//默认显示
                //autoBind: false,//预加载
                //template: '<span>#: data.surname ##: data.name #</span>',//自定义显示字段
                change: function (e) {//切换事件
                    //alert('select改变了');
                }
            };
            //时间控件配置
            vm.dateTimePickerOptions = {
                start: "year", //开始视图 参数有century世纪视图 decade十年视图 year年视图 month月视图
                depth: "year", //设置可以选择视图的深度, 参数有century世纪视图 decade十年视图 year年视图 month月视图
                format: "yyyy/MM/dd hh:mm tt",//要显示的格式
                value: new Date(),//默认值
                change: function () { //值改变时触发
                    var value = this.value();
                    console.log(value); 
                }
            }
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax();
            });

            vm.loading = false;

            vm.permissions = {
                create: abp.auth.hasPermission('Pages.Editions.Create'),
                edit: abp.auth.hasPermission('Pages.Editions.Edit'),
                'delete': abp.auth.hasPermission('Pages.Editions.Delete')
            };

            vm.editionGridOptions = {
                enableHorizontalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                enableVerticalScrollbar: uiGridConstants.scrollbars.WHEN_NEEDED,
                appScopeProvider: vm,
                columnDefs: [
                    {
                        name: app.localize('Actions'),
                        width: 120,
                        cellTemplate:
                            '<div class=\"ui-grid-cell-contents\">' +
                            '  <div class="btn-group dropdown" uib-dropdown="">' +
                            '    <button class="btn btn-xs btn-primary blue" uib-dropdown-toggle="" aria-haspopup="true" aria-expanded="false"><i class="fa fa-cog"></i> ' + app.localize('Actions') + ' <span class="caret"></span></button>' +
                            '    <ul uib-dropdown-menu>' +
                            '      <li><a ng-if="grid.appScope.permissions.edit" ng-click="grid.appScope.editEdition(row.entity)">' + app.localize('Edit') + '</a></li>' +
                            '      <li><a ng-if="grid.appScope.permissions.delete" ng-click="grid.appScope.deleteEdition(row.entity)">' + app.localize('Delete') + '</a></li>' +
                            '    </ul>' +
                            '  </div>' +
                            '</div>'
                    },
                    {
                        name: app.localize('EditionName'),
                        field: 'displayName'
                    },
                    {
                        name: app.localize('CreationTime'),
                        field: 'creationTime',
                        cellFilter: 'momentFormat: \'L\''
                    }
                ],
                data: []
            };

            vm.getEditions = function () {
                vm.loading = true;
                editionService.getEditions({}).success(function (data) {
                    vm.editionGridOptions.data = data.items;
                }).finally(function () {
                    vm.loading = false;
                });
            };

            function openCreateOrEditEditionModal(editionId) {
                var modalInstance = $uibModal.open({
                    templateUrl: '~/App/host/views/editions/createOrEditModal.cshtml',
                    controller: 'host.views.editions.createOrEditModal as vm',
                    backdrop: 'static',
                    resolve: {
                        editionId: function () {
                            return editionId;
                        }
                    }
                });

                modalInstance.result.then(function (result) {
                    vm.getEditions();
                });
            }

            vm.createEdition = function() {
                openCreateOrEditEditionModal(null);
            };

            vm.editEdition = function(edition) {
                openCreateOrEditEditionModal(edition.id);
            };

            vm.deleteEdition = function(edition) {
                abp.message.confirm(
                    app.localize('EditionDeleteWarningMessage', edition.displayName),
                    function(isConfirmed) {
                        if (isConfirmed) {
                            editionService.deleteEdition({
                                id: edition.id
                            }).success(function() {
                                vm.getEditions();
                                abp.notify.success(app.localize('SuccessfullyDeleted'));
                            });
                        }
                    }
                );
            };

            vm.getEditions();
        }
    ]);
})();